package com.xindz.cn.mvptest.app;

import android.app.Application;

import com.avos.avoscloud.AVOSCloud;
import com.avos.avoscloud.AVUser;
import com.blankj.utilcode.util.Utils;
import com.kingja.loadsir.core.LoadSir;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.FormatStrategy;
import com.orhanobut.logger.Logger;
import com.orhanobut.logger.PrettyFormatStrategy;
import com.tencent.bugly.crashreport.CrashReport;
import com.xindz.cn.mvptest.components.product.data.AttachProductBean;
import com.xindz.cn.mvptest.components.product.data.ProductBean;
import com.xindz.cn.mvptest.components.user.data.LZUser;
import com.xindz.cn.mvptest.shared.core.loadsir.callback.EmptyCallback;
import com.xindz.cn.mvptest.shared.core.loadsir.callback.ErrorCallback;
import com.xindz.cn.mvptest.shared.core.loadsir.callback.LoadingCallback;
import com.xindz.cn.mvptest.shared.core.loadsir.callback.TimeoutCallback;

/**
 * @name Base
 * @class name：com.xindz.cn.base.app
 * @describe describe
 * @anthor XINDZ
 * @time 2017/10/25 18:21
 * @change
 * @chang time
 * @class describe
 */
public class App extends Application {
    private static Application application;
    private AppConfig appConfig;
    /**
     * 开发状态 发布需要将他修改为   AppConstant.DEVELOP_STATE_DEBUG;
     */
    private boolean developState;

    public static Application initlaize() {
        return application;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        this.application = this;
        appConfig = AppConfig.getAppConfig();
        developState = appConfig.getDevelopState();
        init();
    }

    private void init() {
        Utils.init(this);
        initLogger(developState);
        initBugly(developState);
        initLeanCloud(developState);
        initLoadSir();
    }

    /**
     * 初始化加载反馈框架
     */
    private void initLoadSir() {
        LoadSir.beginBuilder()
                .addCallback(new ErrorCallback())
                .addCallback(new EmptyCallback())
                .addCallback(new LoadingCallback())
                .addCallback(new TimeoutCallback())
                .setDefaultCallback(LoadingCallback.class)
                .commit();
    }

    /**
     * 初始化LeanCloud
     */
    private void initLeanCloud(boolean state) {
        //注册自定义User
        AVUser.registerSubclass(LZUser.class);
        AVUser.registerSubclass(ProductBean.class);
        AVUser.registerSubclass(AttachProductBean.class);
        //返回的AVUSser自动转为子类
        AVUser.alwaysUseSubUserClass(LZUser.class);
//        AVOSCloud.initialize(this, "5dPGMHoFU57CCsl41B9qNBm2-gzGzoHsz", "nDDE7yWX4NdencQFV7pQCOEX");//公司测试
        AVOSCloud.initialize(this, appConfig.getLEANCLOUD_APP_ID(), appConfig.getLEANCLOUD_APP_KEY());//我的测试
        // 放在 SDK 初始化语句 AVOSCloud.initialize() 后面，只需要调用一次即可
        AVOSCloud.setDebugLogEnabled(state);
    }

    /**
     * 初始化异常收集
     *
     * @param state
     */
    private void initBugly(boolean state) {
        CrashReport.initCrashReport(getApplicationContext(), AppConstant.TenCent.BUGLY_APP_ID, false);
        CrashReport.setIsDevelopmentDevice(this, state);
    }

    /**
     * 初始化日志
     *
     * @param state
     */
    private void initLogger(final boolean state) {
        FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
                .tag(AppConstant.LOG_TAG).build();

        Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy) {
            @Override
            public boolean isLoggable(int priority, String tag) {
                return state;
            }
        });
    }
}
